package com.zprmb.udzt;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.adapter.DexterityShouyiAdapter;
import com.zprmb.udzt.bean.DexterityShouyi_Bean;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;
/**
 * 智投宝收益
 * @author shenshao
 *
 */
public class DexterityShouyiActivity extends BaseActivity implements OnClickListener{
	
	private PullToRefreshListView lv_dexterity_shouyi;
	private ListView actualListView;
	private DexterityShouyiAdapter adapter;
	private ImageView iv_v4_dexterityshouyi_back;
	private TextView tv_v4_dexterityshouyi_title;
	private ArrayList<DexterityShouyi_Bean> dateOfadapter = new ArrayList<DexterityShouyi_Bean>();
	private LinearLayout ll_v4_dexterity_goto;
	private TextView tv_v4_dexterity_goto;
	private int page = 2;
	
	private boolean isyou = true;
	private RelativeLayout rl_back;
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				if (dateOfadapter.size()<1) {
					lv_dexterity_shouyi.setVisibility(View.GONE);
					ll_v4_dexterity_goto.setVisibility(View.VISIBLE);
				}else {
					ll_v4_dexterity_goto.setVisibility(View.GONE);
					lv_dexterity_shouyi.setVisibility(View.VISIBLE);
					adapter = new DexterityShouyiAdapter(DexterityShouyiActivity.this,dateOfadapter);
					actualListView.setAdapter(adapter);
					lv_dexterity_shouyi.onRefreshComplete();
				}
				break;
			case 2:// 下拉
				isyou = true;
				adapter = new DexterityShouyiAdapter(DexterityShouyiActivity.this,dateOfadapter);
				actualListView.setAdapter(adapter);
				lv_dexterity_shouyi.onRefreshComplete();
				break;
			case 3:// 上拉
				adapter.notifyDataSetChanged();
				lv_dexterity_shouyi.onRefreshComplete();
				break;
			case R.id.rl_back:
				finish();
				break;
			}
		};
	};
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.v4dexterity_shouyi_activity);
		 BbtApplication.getInstance().addActvity(this);
		iv_v4_dexterityshouyi_back = (ImageView) findViewById(R.id.img_back);
		iv_v4_dexterityshouyi_back.setOnClickListener(this);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		rl_back.setOnClickListener(this);
		tv_v4_dexterityshouyi_title = (TextView) findViewById(R.id.tv_v4_dexterityshouyi_title);
		tv_v4_dexterityshouyi_title.getPaint().setFakeBoldText(true);
		tv_v4_dexterityshouyi_title.setText("收益明细");
		lv_dexterity_shouyi = (PullToRefreshListView) findViewById(R.id.lv_dexterity_shouyi);
		actualListView = lv_dexterity_shouyi.getRefreshableView();
		ll_v4_dexterity_goto = (LinearLayout) findViewById(R.id.ll_v4_dexterity_goto);
		tv_v4_dexterity_goto = (TextView) findViewById(R.id.tv_v4_dexterity_goto);
		tv_v4_dexterity_goto.setOnClickListener(this);
		initData(1, 1, true);
		lv_dexterity_shouyi.setOnRefreshListener(new OnRefreshListener<ListView>() {
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				page = 2;
				initData(1, 2, false);
			}
		});
		
		lv_dexterity_shouyi.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			public void onLastItemVisible() {
				if (isyou) {
					initData(page, 3, false);
					page++;
				}
			}
		});
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
	private void initData(int page, final int handlerId, boolean isShow){
		AjaxParams params = new AjaxParams();
		params.put("pageNo", page+"");
		params.put("pageSize", "12");
		params.put("flag", "aa");
		VolleryUtil vollery = new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if (jsonObject.optInt("success")==1) {
					Log.e("lincun", jsonObject.toString());
					JSONArray json = jsonObject.optJSONArray("data");
					if (handlerId==2) {
						dateOfadapter.clear();
					}
					if (handlerId==3&&json.length()<1) {
						isyou = false;
					}
					for (int i = 0; i < json.length(); i++) {
						JSONObject object = null;
						try {
							object = (JSONObject) json.get(i);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						DexterityShouyi_Bean item = new DexterityShouyi_Bean();
						item.parseJSON(object);
						dateOfadapter.add(item);
					}
					//v4info.parseJSON_dexterityshouyi(jsonObject.optJSONArray("data"));
					handler.sendEmptyMessage(handlerId);
				} else {
					if (jsonObject.optString("comment").equals("please login")) {
						if(getShare().getString("mobile", "").equals("")){
							Intent intent = new Intent(DexterityShouyiActivity.this,Log_or_RegisActivity.class);
							startActivity(intent);
							DexterityShouyiActivity.this.finish();
						}else{
							
							Intent intent = new Intent(DexterityShouyiActivity.this,LoginActivity.class);
							startActivity(intent);
							DexterityShouyiActivity.this.finish();
						}
					}else{
						Toast.makeText(DexterityShouyiActivity.this, jsonObject.optString("comment"), 2000).show();
					}
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		vollery.setHeaders("token", getToken());
		System.out.println("dexteriyu==="+getToken());
		vollery.setRequestMethod(0);
		vollery.setShow(isShow);
		vollery.execute(this, Constant.URL+"api/v2/interestRecord?", params.toString());
	}
	
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_back:
			finish();
			break;
		case R.id.tv_v4_dexterity_goto:
			finish();
			break;
		case R.id.rl_back:
			finish();
			break;
		}
	}
}
 